package com.kdtech.analyse.NewsPaper;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;
import com.kdtech.analyse.AnalyseNews;
/**
 * 
 * http://country.scol.com.cn/shtml/scncrb/20121204/index.shtml  四川农村日报
 * http://morning.scol.com.cn/new/html/tfzb/20121204/index.html 天府早报
 * http://stocknews.scol.com.cn/shtml/jrtzb/20121204/index.shtml 金融投资报
 * http://wccdaily.scol.com.cn/ 华西都市报
 * 
 * http://homelife.scol.com.cn/new/html/jtshb/20121204/index.html 家庭与生活报
 * http://xzb.scol.com.cn/new/html/xfzlb/20121204/index.html	  消费质量报
 * @author allen 
 */
public class ScolNewsPaperAnalyse implements AnalyseNews {
	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		String[] regex = {
				"http://epaper.scdaily.cn/shtml/scrb/[0-9]{8}/[0-9]*.shtml",
				"http://morning.scol.com.cn/new/html/tfzb/[0-9]{8}/tfzb[0-9]{6}.html",
				"http://country.scol.com.cn/shtml/scncrb/[0-9]{8}/[0-9]*.shtml",
				"http://wccdaily.scol.com.cn/shtml/hxdsb/[0-9]{8}/[0-9]*.shtml",
				"http://www.homelife.sc.cn/new/html/jtshb/[0-9]{8}/jtshb[0-9]*.html",
				"http://market.scol.com.cn/new/html/xfzlb/[0-9]{8}/xfzlb[0-9]*.html",
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {		
		NewsMeta newspaper = new NewsMeta();
				
		String html = urlMeta.getHtml();
		String url = urlMeta.getUrl();	
				
		newspaper.setUrl(url);		
		
		String title = null;
		String content = null;
		Long date = null;
		String dateStr = null;
		Document doc = Jsoup.parse(html);
	

		title =doc.select("h3").text();
		if(StringUtils.isBlank(title)){
			title=doc.select("title").text();
			title=title.replace("— 天府早报 - 四川在线", "");
			title=title.replace("— 家庭生活报 - 四川在线", "");
			title=title.replace(" — 消费质量报 - 四川在线", "");
			if(url.startsWith("http://country.scol.com.cn")){
				if(title.indexOf("第")!=-1){
					title=title.substring(0,title.indexOf("第"));
				}
			}
		}	
		
		date=DateUtils.matchDate(url);
		
		content =HtmlCleaner.getContentHtml(url,doc.select("div#frameContent"));
		if(StringUtils.isBlank(content)){
			content =HtmlCleaner.getContentHtml(url,doc.select("html body center table#Table1 tbody tr td table#Table3 tbody tr td table#Table10 tbody tr td table#Table14 tbody tr td table#Table15 tbody tr td table#Table17 tbody tr td"));
			if(StringUtils.isBlank(content)){
				content =HtmlCleaner.getContentHtml(url,doc.select("html body table tbody tr td table.border-grey tbody tr td#DivDisplay.f-14 div.f-14"));
			}
		}	
		
		newspaper.setTitle(StringUtils.trimSpace(title));
		newspaper.setContent(StringUtils.trimSpace(content));
		newspaper.setDate(date);

		return newspaper;
	}
	
	
	public boolean isTaskPage(String url) {
		boolean bRet = false;
		String[] regex = {
				"http://epaper.scdaily.cn/shtml/scrb/[0-9]{8}/v[0-9]*.shtml",
				"http://morning.scol.com.cn/new/html/tfzb/[0-9]{8}/tfzb[0-9]*.html",
				"http://country.scol.com.cn/shtml/scncrb/[0-9]{8}/v[0-9]*.shtml",
				"http://www.homelife.sc.cn/new/html/jtshb/[0-9]{8}/jtshba[0-9]*.html",
				"http://market.scol.com.cn/new/html/xfzlb/[0-9]{8}/xfzlb[0-9]*.html"
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}
	
	
	

	
	
}
